package com.wyp.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyp.pojo.Testpapers;
import com.wyp.service.impl.QuestionsServiceImpl;
import com.wyp.service.impl.TestpapersServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class TestpaperExamineController {

    @Autowired
    private TestpapersServiceImpl testpapersService;

    @Autowired
    private QuestionsServiceImpl questionsService;

    @RequestMapping("/testpaper_examine.html")
    public String tologin(Model model,
                          @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "4") int pageSize) {
        if (pageNum <= 0) {
            pageNum = 1;
        }
        //1.引入分页插件,pageNum是第几页，pageSize是每页显示多少条,默认查询总数count
        PageHelper.startPage(pageNum, pageSize);
        //2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页，除非再次调用PageHelper.startPage
        try {
            List<Map<String, Object>> testpapersList = testpapersService.qureyTestpapersExam();
            //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(testpapersList, pageSize);
            //4.使用model/map/modelandview等带回前端
            model.addAttribute("pageInfo", pageInfo);
        } finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }
        return "admin/testpaper/testpaper_examine";
    }

    @RequestMapping("/testpaper_examineop.html/{id}")
    public String totpinfo(
            @PathVariable("id") int id,
            Model model
    ) {
        Map<String, Object> testpaper = testpapersService.qureyTestpapersByid(id);
        String content = (String) testpaper.get("testpapercontent");
        String[] questionsid = content.split(",");
        List<Map<String, Object>> Squestions = new ArrayList<>();
        List<Map<String, Object>> Mquestions = new ArrayList<>();
        List<Map<String, Object>> Jquestions = new ArrayList<>();
        for (String questionid : questionsid) {
            if (!questionid.equals("")) {
                Map<String, Object> question = questionsService.qureyQuestionsByid(Integer.parseInt(questionid));
                if ((int)question.get("type") == 1) {
                    Squestions.add(question);
                }
                if ((int)question.get("type") == 2) {
                    Mquestions.add(question);
                }
                if ((int)question.get("type") == 3) {
                    Jquestions.add(question);
                }
            }
        }
        model.addAttribute("testpaper", testpaper);
        model.addAttribute("Squestions", Squestions);
        model.addAttribute("Mquestions", Mquestions);
        model.addAttribute("Jquestions", Jquestions);
        return "admin/testpaper/testpaper_examineop";
    }

    @GetMapping("/testpaper_examrefuse.html/{id}")
    public String testpaperrefuse(
            @PathVariable("id") int id
    ) {
        Map<String, Object> testpaper = testpapersService.qureyTestpapersByid(id);
        Testpapers temp = new Testpapers();
        temp.setTestpaperid((int)testpaper.get("testpaperid"));
        temp.setApartmentid((int)testpaper.get("apartmentid"));
        temp.setTestpapername((String)testpaper.get("testpapername"));
        temp.setSinglenum((int)testpaper.get("singlenum"));
        temp.setMultiplenum((int)testpaper.get("multiplenum"));
        temp.setJudgenum((int)testpaper.get("judgenum"));
        temp.setStatus(3);
        temp.setStarttime((Date)testpaper.get("starttime"));
        temp.setEndtime((Date)testpaper.get("endtime"));
        temp.setTestpapercontent((String)testpaper.get("testpapercontent"));
        temp.setType((boolean)testpaper.get("type"));
        temp.setTotalpoint((int)testpaper.get("totalpoint"));
        testpapersService.updateTestpapers(temp);
        return "redirect:/admin/testpaper_examine.html";
    }

    @PostMapping("/testpaper_examreacess.html")
    public String testpaperacess(
            Testpapers testpaper,
            @RequestParam("Tstarttime") String Tstarttime,
            @RequestParam("Tendtime") String Tendtime,
            @RequestParam("timelimt") int timelimt
    ) throws ParseException {
        Map<String, Object> testpapers = testpapersService.qureyTestpapersByid(testpaper.getTestpaperid());
        Testpapers temp = new Testpapers();
        temp.setTestpaperid((int)testpapers.get("testpaperid"));
        temp.setApartmentid((int)testpapers.get("apartmentid"));
        temp.setTestpapername((String)testpapers.get("testpapername"));
        temp.setSinglenum((int)testpapers.get("singlenum"));
        temp.setMultiplenum((int)testpapers.get("multiplenum"));
        temp.setJudgenum((int)testpapers.get("judgenum"));
        temp.setTestpapercontent((String)testpapers.get("testpapercontent"));
        temp.setType((boolean)testpapers.get("type"));
        temp.setTotalpoint((int)testpapers.get("totalpoint"));
        String[] starttimes = Tstarttime.split("T");
        String[] endtimes = Tendtime.split("T");
        String starttime = starttimes[0] + " " + starttimes[1];
        String endtime = endtimes[0] + " " + endtimes[1];
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        temp.setStarttime(format.parse(starttime));
        temp.setEndtime(format.parse(endtime));
        temp.setTimelimt(timelimt);
        temp.setStatus(2);
        testpapersService.updateTestpapers(temp);
        return "redirect:/admin/testpaper_examine.html";
    }

    @RequestMapping("/testpaper_qureycheck.html")
    public String toqureytestlibinfo(
            @Nullable @RequestParam("qureymessage") String qureymessage
    ) throws UnsupportedEncodingException {
        if (qureymessage != null && !(qureymessage.equals(""))) {
            qureymessage = new String(qureymessage.getBytes("utf-8"), "iso-8859-1");
            return "redirect:/admin/testpaper_qureycheck.html/" + qureymessage;
        } else {
            return "redirect:/admin/testpaper_examine.html";
        }
    }

    @RequestMapping("/testpaper_qureycheck.html/{qureymessage}")
    public String qureytestlibinfo(
            Model model,
            @Nullable @PathVariable("qureymessage") String qureymessage,
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "4") int pageSize
    ) {
        if (pageNum <= 0) {
            pageNum = 1;
        }
        //1.引入分页插件,pageNum是第几页，pageSize是每页显示多少条,默认查询总数count
        PageHelper.startPage(pageNum, pageSize);
        //2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页，除非再次调用PageHelper.startPage
        try {
            if (qureymessage == null || qureymessage.equals("")) {
                List<Map<String, Object>> testpapers = testpapersService.qureyTestpapersExam();
                PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(testpapers, pageSize);
                model.addAttribute("pageInfo", pageInfo);
            } else {
                List<Map<String, Object>> testpapers = testpapersService.qureyTestpapersByExampapername(qureymessage);
                //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
                PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(testpapers, pageSize);
                //4.使用model/map/modelandview等带回前端
                model.addAttribute("pageInfo", pageInfo);
            }
        } finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }
        return "admin/testpaper/testpaper_examine";
    }

}
